package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Learner
 * @Version: 1.0
 */
@Service
public class NursingServiceImpl implements NursingService {
    @Autowired
    private NursingMapper nursingMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    //查询所有护理计划
    @Override
    public List<NursingPlan> getPlan() {
        return nursingMapper.getPlan();
    }

    //新增
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursing(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingMapper.addNursing(nursingPlan);
        Long id = nursingPlan.getId();


        List<NursingProjectPlanDto> nursingProjectPlans = nursingPlanDto.getProjectPlans();
        List<NursingProjectPlan> projectPlan = new ArrayList<>();
        //将NursingProjectPlanDto对象中的数据转换到NursingProjectPlan中去
        for (NursingProjectPlanDto dto : nursingProjectPlans) {
            NursingProjectPlan plan = new NursingProjectPlan();
            BeanUtils.copyProperties(dto, plan);
            projectPlan.add(plan);
        }
        if (projectPlan != null && projectPlan.size() > 0) {

            projectPlan.forEach(projectPlans -> projectPlans.setPlanId(id));

            nursingProjectPlanMapper.insertBatch(projectPlan);
        }
    }

    //条件分页查询
    @Override
    public PageResponse<NursingPlan> getSearch(String planName, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlans = nursingMapper.getSearch(planName, status);
        return PageResponse.of(nursingPlans, NursingPlan.class);
    }

    //2.4 根据ID查询
    @Override
    public NursingPlanVo getById(long id) {
        return nursingMapper.getById(id);
    }

    //2.5修改
    @Override
    public void updateById(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingMapper.updateById(nursingPlan);

        //删除旧的护理项目信息
        Long id = nursingPlanDto.getId();
        nursingProjectPlanMapper.delByPlanId(id);
        //添加新的护理项目信息
        List<NursingProjectPlanDto> nursingProjectPlans = nursingPlanDto.getProjectPlans();
        List<NursingProjectPlan> projectPlan = new ArrayList<>();
        //将NursingProjectPlanDto对象中的数据转换到NursingProjectPlan中去
        for (NursingProjectPlanDto dto : nursingProjectPlans) {
            NursingProjectPlan plan = new NursingProjectPlan();
            BeanUtils.copyProperties(dto, plan);
            projectPlan.add(plan);
        }
        if (projectPlan != null && projectPlan.size() > 0) {

            projectPlan.forEach(projectPlans -> projectPlans.setPlanId(id));

            nursingProjectPlanMapper.insertBatch(projectPlan);
        }

    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delById(long id) {
        nursingMapper.delById(id);
        nursingProjectPlanMapper.delByPlanId(id);
    }

    @Override
    public void updateStatus(long id, Integer status) {
        nursingMapper.updateStatus(id, status);
    }


}
